#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/6/10 15:30
# @function: 快速将仅两列的txt文件读入excel中
# @version : V1

import os
from openpyxl import Workbook
from tkinter import filedialog as fd, messagebox, filedialog


class Reader:
    def __init__(self):
        self.absolute = False
        self.lines = None
        self.txt_files = filedialog.askopenfilenames()
        self.wb = Workbook()
        self.ws = self.wb.active
        self.head = dict()
        self.split_note = ","

    def handler(self):
        for file_index,file in enumerate(self.txt_files):
            self.ws.cell(row=1,column=file_index+1,value=os.path.basename(file))
            with open(file, 'r', encoding='utf-8') as f:
                for line_index, line in enumerate(f.readlines()):
                    if self.absolute is True:
                        self.ws.cell(row=line_index + 2, column=file_index + 1,
                                     value=abs(float(line.strip().split(self.split_note)[-1])))  # 数据data
                    if self.absolute is False:
                        self.ws.cell(row=line_index + 2, column=file_index + 1,
                                     value=float(line.strip().split(self.split_note)[-1]))  # 数据data
        savepath = filedialog.asksaveasfilename(filetypes=[("Excel files", "*.xlsx")],defaultextension=".xlsx")
        self.wb.save(savepath)



if __name__ == '__main__':
    reader = Reader()
    reader.absolute = True
    reader.handler()